package com.solr.web.utils;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;

/**代理存在情况下的Http的相关工具类。
 */
public final class HttpProxyUtils {

    /**
     * 构建一个空的<code>HttpProxyUtils.java</code>
     */
    private HttpProxyUtils() {
        super();
    }

    /**
     * 取得真实的请求方IP
     * @param request HttpServletRequest
     * @return 真实的请求方IP
     */
    public static String getRealRemoteAddr(final HttpServletRequest request) {
        final String xForwardedForVal = request.getHeader("x-forwarded-for");
        if (StringUtils.isNotEmpty(xForwardedForVal)) {
            final String[] forwardeds = xForwardedForVal.split(",");
            return forwardeds[0].trim();
        }
        final String xRealIp = request.getHeader("x-real-ip");
        if (StringUtils.isNotEmpty(xRealIp)) {
            return xRealIp.trim();
        }
        return request.getRemoteAddr();
    }
    
    /**
     * 取得真实的请求Host
     * @param request HttpServletRequest
     * @return 真实的请求Host
     */
    public static String getRealRemoteHost(final HttpServletRequest request) {
        final String host = request.getHeader("host");
        if (StringUtils.isNotEmpty(host)) {
            return host;
        }
        return request.getRemoteHost();
    }

}
